package pack.be;

import java.sql.SQLException;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ProjectListView extends ListActivity{
	
	private ListView lvListe;
	private Button disConnectBut;
	private TextView userName;
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.projectsview);
        
        try {
			remplirListe();
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, listeProjects));
        
        lvListe = getListView();
        lvListe.setTextFilterEnabled(true);
        
        //Show a pop up to notice that user is connected now
        Toast t = Toast.makeText(getApplicationContext(), "Congrat's you are connected!", Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
        
        disConnectBut = (Button)findViewById(R.id.disConnectBut);
        
        disConnectBut.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				Intent intent = new Intent(ProjectListView.this, BEAndroidActivity.class);
				startActivity(intent);
			}
		});
        	
        //si on clique sur un projet on doit pouvoir acceder a la liste de ses indicateurs
        lvListe.setOnItemClickListener(new OnItemClickListener() {
  	    public void onItemClick(AdapterView<?> parent, View view,
  	        int position, long id) {
  	      // acceder a la liste d'indicateur 
  	    	Intent intent = new Intent(ProjectListView.this, ListIndicats.class);
  	    	startActivity(intent);
  	    }
  	  });
        
    }
    
    //recuperer les projets ds la bdd
    public String[] listeProjects;
    
    public void remplirListe() throws Exception{
    	ConnectionPostgre con = new ConnectionPostgre();
    	con.connection();
    	String resultat[];
    	int cont = 0, i = 0;
    	
    	resultat = con.listeProjets("Pierre");
    	listeProjects = new String[(resultat.length/2)];
    	while(cont < listeProjects.length){
    		listeProjects[i] = resultat[cont];
    		cont = cont + 2;
    		i++;
    	}
    	
    	con.deconnection();
    }
}
